function InitFiltersForm()
{
  var d = new Date();
  d.setMonth(d.getMonth()-4, 30);
  document.getElementById('dateto').value = FormatDate(d); //initialize datefrom  with actual date
  d.setMonth(d.getMonth(), 1);
  document.getElementById('datefrom').value = FormatDate(d);   //initialize dateto  with actual date
    
  $(function() 
  {
    $('#datefrom').datepicker({ dateFormat: 'yy-mm-dd' });
    $('#dateto').datepicker({ dateFormat: 'yy-mm-dd' });
  });
  
  CheckResponseStatus();
}
//Create categories/products OVERVIEW

function PopulateTableResults(obj)
{
  
  var requestsInfoElement = document.getElementById('Data_table');
  var TableRowsHTML = '';
  var i=0;

  for (var key in obj)
  {
    if(i<10)
    {
      TableRowsHTML += '<tr> <td rowspan = "3">' + key + '</td>'

      +'<td>MAX</td>'
      +'<td>' + obj[key]['data']['Max']['value']  + '</td>'
      +'<td>' + obj[key]['data']['Max']['date']  + '</td></tr>'

      +'<tr><td>MIN</td>'
      +'<td>' + obj[key]['data']['Min']['value']  + '</td>'
      +'<td>' + obj[key]['data']['Min']['date']  + '</td></tr>'

      +'<tr><td>Total</td>'
      +'<td colspan = "2">' + obj[key]['data']['Total'] + '</td>';


      requestsInfoElement.innerHTML = '<table id="requests-info-table" border="1">'
      + '<tr>'
      + '<th class="green">Category</th>'
      + '<th class="green">Options</th>'
      + '<th class="green">Views</th>'
      + '<th class="green">Period</th>'
      + '</tr>'
      + TableRowsHTML + '</table>';
      i++;
    }
  }
        
}

function GetDaysNumber(dateFrom, dateTo, timeunit)
{
  
  if(timeunit == 'day')
  {
    var diff =  Math.round(( Date.parse(dateTo) - Date.parse(dateFrom) ) / 86400000);

    if(diff<=6)
      return 1;
    else
    return Math.round(diff/14); 
  }
  
  if(timeunit == 'week')
  {
    var diff =  Math.floor(( Date.parse(dateTo) - Date.parse(dateFrom) ) / 604800000); 
    if(Math.round(diff)<10)
    return 1;
    else return Math.round(diff/15);
  }
  if(timeunit == 'month')
  {
    
    return 1; 
  }
    
}

function GetFlotTicks(obj)
{
  var pointer = [];
  var i=0;
  
 
  for (var key in obj) 
  {
    var nr_par=0;
    if(i<1)
    {
      for (var keys in obj[key])
      {
        if(nr_par % GetDaysNumber(REQUEST_DATA.datefrom, REQUEST_DATA.dateto, REQUEST_DATA.timeunit) == 0){
        var point = [];
        
        point[0] = new Date(obj[key][keys]['range-start']).getTime();
        point[1] = FormatFlotDate(new Date(obj[key][keys]['range-start']) , new Date(obj[key][keys]['range-end']));
        
        pointer.push(point);
        }
        
        nr_par ++;
      }
      i++;
    }
  }
  
return pointer;
}

function FormatFlotDate(datefrom, dateto){
  var monthFrom,monthTo, dayFrom, dayTo;
  
  monthFrom = shortMonthNames[datefrom.getMonth()];
  monthTo = shortMonthNames[dateto.getMonth()];
  
  dayFrom = (datefrom.getDate());
  dayTo = (dateto.getDate());
  
   if(REQUEST_DATA.timeunit != 'day')
   {
    if(monthFrom == monthTo)
    {
      return dayFrom + " - " + dayTo + " " + monthFrom;       
    }
    else
      return dayFrom + " " + monthFrom + " - " + dayTo + " " + monthTo;
   }
  if(REQUEST_DATA.timeunit == 'day')
    {
      return dayFrom + " " + monthFrom;
      
    }
}

function FormatDate(date){
  var m, d, y;
 
  if(date.getMonth()<9)
    m = "0"+(date.getMonth()+1);
  else 
    m = (date.getMonth()+1);
  
  if(date.getDate()<10)
    d = "0"+(date.getDate());
  else 
    d = (date.getDate()); 
  
  y = date.getFullYear();
    if (y<2011)
    y= 2011;
  return y+"-"+m+"-"+d;
  
}

function GetPreviousWeek(date)
{
  var y, m, d;
  
  y = date.getUTCFullYear();
  m = date.getUTCMonth();
  d = (date.getUTCDate()-6);

  return new Date(y, m, d);
}

function GetNextWeek(date)
{
  var y, m, d;
  
  y = date.getUTCFullYear();
  m = date.getUTCMonth();
  d = (date.getUTCDate()+7);
  
  return new Date(y, m, d);
}

function GetPreviousMonth(date)
{
  var y, m, d;
  
  y = date.getUTCFullYear();
  m = date.getUTCMonth() - 1;
  date.setDate(1);
  d = date.getUTCDate();
  if (m < 0) {y--;m = 11}
  
  return new Date(y, m, d);
}

function GetNextMonth(date)
{
  var y, m, d;
  
  y = date.getUTCFullYear();
  m = date.getUTCMonth()+1;
  d = date.getUTCDate();
  if (m > 12) {y++;m = 1}
  
  return new Date(y, m, d);
}


function DatePeriod(condition)
{
  var string, sDate, date;
  
  switch(condition)
  {
    case 'prevMonth':
      string = document.getElementById('datefrom').value;
      sDate = new Date(Date.parse(string));
      date = new Date(GetPreviousMonth(sDate));
      sDate.setMonth(sDate.getMonth());
      sDate.setDate(0);
      document.getElementById('dateto').value= FormatDate(sDate);
      document.getElementById('datefrom').value = FormatDate(date);
    break;
    
    case 'nextMonth':
      string = document.getElementById('dateto').value;
      sDate = new Date(Date.parse(string));
      sDate.setDate(1);
      sDate.setMonth(sDate.getMonth()+1);
      document.getElementById('datefrom').value= FormatDate(sDate);
      date = new Date(GetNextMonth(sDate));
      date.setDate(0);
      document.getElementById('dateto').value = FormatDate(date);
    break;
    
    case 'prevWeek':
      string = document.getElementById('datefrom').value;
      sDate = new Date(Date.parse(string));
      sDate.setDate(sDate.getDate()-sDate.getDay());
      document.getElementById('dateto').value= FormatDate(sDate);
      date = new Date(GetPreviousWeek(sDate));
      document.getElementById('datefrom').value = FormatDate(date);
    break;
    
    case 'nextWeek':
      string = document.getElementById('dateto').value;
      sDate = new Date(Date.parse(string));
      if(sDate.getDay()==0)
        sDate.setDate(sDate.getDate()-sDate.getDay()+1);
      else
      sDate.setDate(sDate.getDate()-sDate.getDay()+7);
      document.getElementById('datefrom').value= FormatDate(sDate);
      date = new Date(GetNextWeek(sDate));
      date.setDate(date.getDate()- sDate.getDay());
      document.getElementById('dateto').value = FormatDate(date);
    break;
    
  }
  
  CheckResponseStatus();
}